<template lang="pug">
  .page
    .lzw-section
      .tab-title
        .font-14(v-for="(item,index) in filterNav" :key="index" @click="filterMethod(index,$event)" :data-status="item.status" :class="tabIndex === index ? 'active' : ''") {{item.txt}}
      .tab-container.pt-20
        .handle-box.clearfix.pr-20.pl-20
          .pull-left
            span.leftText
              | 用户：
              span.text-info.pl-5.pr-5.font-weight {{totalcount}}
              | 个
          .pull-right
            Button.mybtn-2(type="info" @click="create").ml-15 新建用户
            template(v-if="tabIndex === 0")
              Button.mybtn-2(type="info" @click="reward").ml-15 用户奖励
            template(v-else)
              Button.mybtn-2(type="info" @click="pass").ml-15 审核通过
        .pall-20
          Table.beautiful-table(width="100%" stripe :columns="thead" :data="listData" height="500" :loading="loading" @on-selection-change="tableSelectChange")
            template(slot-scope="{ row, index }" slot="sex")
              span {{row.sex === 1 ? '男' : '女'}}
            template(slot-scope="{ row, index }" slot="totalEvaluation")
              Rate(show-text allow-half disabled v-model="row.orderCount.tableSelectChange")
                span(style="color:#f5a623") {{row.orderCount.tableSelectChange}}
            template(slot-scope="{ row, index }" slot="action")
              Button.mr-10(type="text" size="small" @click="checkInfo(row.id)") 查看用户信息
            .clearfix.pl-20.pr-20(slot="footer")
              Page(:total="totalcount" :current="pageIndex" :page-size="pageSize" @on-change="pageChange" show-total)
    createModal(ref="createModal" :sex_arr="sex_arr" :degreeArr="degreeArr" :idcardtype_arr="idcardtype_arr" @get-list="getAllWorker")
    checkModal(ref="checkModal" :sex_arr="sex_arr" :degreeArr="degreeArr" :idcardtype_arr="idcardtype_arr" :formValidate="formValidate" @get-list="getAllWorker")
</template>

<script>
import userMixin from './userMixin.js'
import createModal from './modal/create.vue'
import checkModal from './modal/check.vue'
import vOrderApi from '@/api/v_order.api.js'
import dataApi from '@/api/data.js'

export default {
  name: 'v_user',
  components: {
    createModal,
    checkModal
  },
  data () {
    return {
      tabIndex: 0,
      auditStatus: '1', // 用户状态 0-未审核 1-审核通过
      
      filterNav: [
        {
          txt: '已入驻用户',
          status: '1'
        },
        {
          txt: '申请入驻用户',
          status: '0'
        }
      ],
      thead: [],
      listData: [],
      ids: [],
      loading: false,
      totalcount: 0,
      pageIndex: 1,
      pageSize: 10,
      createModal: false,
      formValidate: {
        DataSource: '1',
        AuditStatus: '1',
        workExperience: '',
        postExperience: '',
        ifStudent: '',
        id: '', // 员工ID
        companyId: '', // 公司ID
        companyName: '', // 公司名称
        insuranceId: '', // 保险ID
        name: '',
        eName: '',
        idType: '0',
        idCardNo: '',
        otherIdCardNo: '',
        birthDay: '',
        sex: '',
        age: '',
        race: '',
        birthplace: '',
        idCardStartTime: '',
        idCardEndTime: '',
        idCardAddress: '',
        residentAddress: '',
        Phone: '',
        email: '',
        emergencyContact: '',
        emergencyContactPhone: '',
        maritalStatus: '',
        hasChildren: '',
        professionalTitle: '',
        degree: '',
        schoolName: '',
        graduationDate: '',
        discipline: '',
        onBoardingDate: '',
        department: '',
        duty: '',
        employeeNo: '',
        employeeType: 'service',
        employeeTypeName: '劳务工',
        beHiring: 'true',
        city: [],
        cityStr: '',
        accountName: '',
        bankName: '',
        accountNo: '',
        salaryCard: {
          accountName: '',
          bankName: '',
          accountNo: ''
        }
      },
      sex_arr: [],
      degreeArr: [], // 学历
      idcardtype_arr: [], // 证件类型
    }
  },
  mixins: [userMixin],
  created () {
    this.getEnum()
    // this.filterMethod(0)
    let thead_extend = ['selection', 'createTime', 'name', 'idCardNo', 'assignOrder', 'meetOrder', 'completeOrder', 'failOrder', 'processingOrder', 'sumAmount', 'totalEvaluation', 'userStatus', 'action']
    this.thead = this.getTableColumns(thead_extend)
    this.getAllWorker()
  },
  methods: {
    filterMethod (index, e) {
      this.tabIndex = index
      this.auditStatus = e.target.dataset.status
      if (index) {
        let thead_extend = ['selection', 'name', 'sex', 'age', 'degree', 'residentAddress', 'workExperience', 'postExperience', 'apply_time']
        this.thead = this.getTableColumns(thead_extend)
      } else {
        let thead_extend = ['selection', 'createTime', 'name', 'idCardNo', 'assignOrder', 'meetOrder', 'completeOrder', 'failOrder', 'processingOrder', 'sumAmount', 'totalEvaluation', 'userStatus', 'action']
        this.thead = this.getTableColumns(thead_extend)
      }
      this.getAllWorker()
    },
    // 获取用户
    getAllWorker () {
      vOrderApi.getAllWorker({
        DataSource: 1,
        AuditStatus: this.auditStatus,
        PageIndex: this.pageIndex,
        PageSize: this.pageSize
      }).then(res => {
        if (res.data.success) {
          this.listData = res.data.data
          this.totalcount = res.data.totalRows
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    tableSelectChange (val) {
      console.log(val)
      this.ids = val.map(item => {
        return item.id
      })
    },
    pageChange (index) {
      this.pageIndex = index
    },
    checkInfo (id) {
      this.$refs.checkModal.modalOpen()
      this.getDetail(id)
    },
    getDetail (id) {
     vOrderApi.GetEmployee({
       id: id
     }).then(res => {
       console.log(res)
       this.formValidate = res.data.data
     })
    },
    create () {
      this.$refs.createModal.modalOpen()
    },
    reward () {
      if (this.ids.length > 0) {
        this.$Message.success('成功')
      } else {
        this.$Message.warning('请选择需要奖励的用户')
      }
    },
    pass () {
      if (this.ids.length > 0) {
        vOrderApi.auditVworker({
          EmployeeIds: this.ids,
          Userid: JSON.parse(localStorage.getItem('clientLoginInfo')).data.userId
        }).then(res => {
          if (res.data.success) {
            this.$Message.success(res.data.message)
            this.getAllWorker()
          } else {
            
            this.$Message.error({
              content: res.data.message,
              duration: 10,
              closable: true
            })
          }
        })
      } else {
        this.$Message.warning('请选择需要通过审核的用户')
      }
    },
    getEnum () {
      let groupNames = 'employee.degree,employee.sex,employee.idcardtype'
      dataApi.getEnum({
        groupName: groupNames
      }).then(res => {
        this.degreeArr = this.getItems(res.data, 'employee.degree')
        this.sex_arr = this.getItems(res.data, 'employee.sex')
        this.idcardtype_arr = this.getItems(res.data, 'employee.idcardtype')
      })
    },
    getItems (arr, key) {
      let object = arr.find((item) => {
        return item.group === key
      })
      return object.items
    }
  }

}
</script>

<style lang="less">

</style>
